home *** CD-ROM | disk | FTP | other *** search
-
-
-
-
- When the AmigaWorld Tech Journal stop publication, I was working on an
- article for the next issue, _Writing a 2.0 command_. I decided to
- do a command that would be fairly useful, and use many of the new 2.0
- AmigaDOS features. So I decided on a Foreach command, sort of like
- an improved LIST LFORMAT option. Well, I still want to sell the article
- sometime (which includes the source to the command) but I figured,
- why not release the command on its own ?
-
- Anyway, here it is..
-
-
- andy
-
-
-
-
-
- Copyright 1992 Andy Finkel
-
-
-
- NAME: FOREACH
- TEMPLATE: NAME, PATH/K, IN/M, FROM/K/N, TO/K/N, STEP/K/N, SCRIPT/K, COM/K,
- WITH/K, ALL/S,FILES/S, DIRS/S, SORT/S, SINCE/K, UPTO/K
-
- PURPOSE: To perform a script loop using local variables to contain loop
- values.
-
- The Foreach command allows you to loop within scripts with a loop variable
- set to a value selected from a list of names. Foreach will execute a script
- of your choice; if no script is given, the foreach command allows you to type
- a temporary one in from the command line. If used from within a script, the
- foreach command will loop within the script.
-
- The foreach loop is bounded by a END directive (or an EOF, or a control-\).
-
- The foreach loop is executed once for each name specified by the IN keyword.
- Full pattern matching is supported. Names specified with the IN option don't
- necessarily have to actually exist as files or directories to be used,
- which allows you to create files.
-
- The NAME keyword allows you to pick a variable name; each time through the
- loop, that variable is set to the name currently selected from the list of
- names specified with the IN keyword.
-
- If no name is specified, no local variables will be created.
-
- The PATH keyword allows you to select a local variable name where the PATH
- will be stored; if no PATH keyword is specified, this will be stored in a
- variable with the same name as NAME but with .PATH appended.
-
- The basename and extension local variables work in a similar manner, except
- that the names are always based on the name specied by the NAME keyword with
- an appended extension. The extension for the basename of the file found
- is .BASE The extension (if any) will be found in the variable name with
- the .EXT extension.
-
- The nametype local variable contains the type of thing, either dir
- (for directory), file (for file), or name (for couldn't find one of those).
-
- Remember, to access variables that contain non-alphanumeric characters
- (like a .) on the command line (like a .) you have to surround the name
- with { } like: ${i.base}
-
- The ALL keyword causes any pattern matching used in any member of the IN
- namelist to be recursive.
-
- The FILES keyword is a 'files only' keyword. When this is used, only files
- (non-directories) will be selected from the list of IN names.
-
- The DIRS keyword is a 'directories only' keyword. When this is used, only
- directories (non-files) will be selected from the list of IN names.
-
- In either the file only or the directory only mode all names which are
- neither files or directories (ie names that don't exist) are selected as well.
-
- Even if the FILES only keyword is used, if the ALL option is specified,
- directories _will_ be entered.
-
- Note: directories are listed _last_ after all files (and/or subdirectories)
- in them are used. (This allows the Foreach command to be used as an interactive
- Delete command.)
-
- The COM keyword allows you to specify a single command to execute. Remember
- to use quotes around the entire command; if one of the arguments
- inside requires quotes, you must escape those quotes using the *. Within
- the COM argument, all variables must be preceeded by an additional $, to
- avoid expansion on the command line before the command is executed.
- By using *N within the COM string, you can define multi-command COM arguments,
- which is especially useful when making aliases that use the foreach command.
-
- The SCRIPT keyword allows you to specify a script file to excute. If no
- script keyword is used (and no COM keyword is specified) you will be
- prompted to create a temporary script.
-
- The WITH keyword allows you to specify a file containing a list of names.
- This list is used after any command line names are used.
-
-
- The FROM , TO and (optional) STEP keywords allow you to specify a numeric range;
- the loop variable will be set to each of the values in the range
- in turn. Both are required to be specified to use this feature. The
- STEP keyword is optional; it allows you to pick an increment other than 1.
- FROM, TO, and STEP can be negative. STEP cannot be zero, however.
-
- The SORT keyword causes the foreach command to do an alphanumeric sort on
- the name list before doing any script execution.
-
-
- The SINCE and UPTO keywords are for date comparisons; SINCE will limit
- operations to files/directories since (and including) the specified date/time.
- UPTO will limit operations to files/directories up to (and including)
- the specified date/time. The usual AmigaDOS shortcuts (today, yesterday, and so on)
- are allowed.
-
-
- The foreach command nests. WARNING: The same variable name should not be
- used for nested Foreach commands; make sure the inner loop variable name is
- not the same as the outside loop variable name.
-
- The foreach command places its temporary files in T: If T:"If not assigned,
- :T is used instead. In that case, if :T does not exist it will be created.
-
-
- BUGS:
- Its sometimes hard to stop the Foreach command from the Shell.
- Many things are looking at the signal bits, and the Foreach command is low
- on the chain. Its often easier to go to another Shell, use Status to find
- out the process number of the Foreach command, and send that process a
- BREAK ALL.
-
- (like this, for instance:
-
- break `status com=foreach` all
-
- )
-
- This behavior could be fixed by using the RunCommand AmigaDOS call, rather
- than the System() call. Perhaps it will in a future version.
-
- EXAMPLES:
-
- foreach i in qwe1 qwe2 ram:#$
- echo "file is $i, path is ${i.path}"
- foreach j in test1 test2
- echo "subname is $j"
- echo "Current local vars are:"
- set
- end
- end
-
-
-
- Here's a handy alias using foreach that gives an interactive delete command.
-
- (note: the alias must be defined on one line, though its shown on two for clarity)
-
- alias del foreach i []
- com "failat 21*Nask *"delete $i ?*"*N if warn *Ndelete ${i.path}$i*N endif"
-
-
-
- ; rename all the .a files to .asm
- foreach i in #?.a
- rename $i ${i.base}.asm
- end
-